var businessTypeData = [{text:"安装量",value:"1"},{text:"维修量",value:"2"},{text:"遗失量",value:"3"},{text:"报废量",value:"4"}];
var groupFieldData = [{text:"按天",value:"UPDATE_DAY"},{text:"按周",value:"UPDATE_WEEK"},{text:"按月",value:"UPDATE_MONTH"}];
mini.parse();
var projectName = $("#projectName").val();
var urlpam = {pageSize:10,pageIndex:0};
setDefaultDate();
var chart_install = echarts.init(document.getElementById("main_install"));
var chart_repair = echarts.init(document.getElementById("main_repair"));
var chart_lose_shop = echarts.init(document.getElementById("main_lose_shop"));
var chart_lose_storage = echarts.init(document.getElementById("main_lose_storage"));
var chart_lose_ing = echarts.init(document.getElementById("main_lose_ing"));
var chart_scrap = echarts.init(document.getElementById("main_scrap"));

var option_install = buildOption("安装量",[{type:"category",name:"日期",data:[]}],[{type:"value",name:"数量"}],{data:["店铺数","设备数"]},[{name:"数量",data:[], type:"line"}]);
var option_repair = buildOption("维修量",[{type:"category",name:"日期",data:[]}],[{type:"value",name:"数量"}],{data:["店铺数","设备数"]},[{name:"数量",data:[], type:"line"}]);
var option_lose_shop = buildOption("店铺丢失量",[{type:"category",name:"日期",data:[]}],[{type:"value",name:"数量"}],{data:["店铺数","设备数"]},[{name:"数量",data:[], type:"line"}]);
var option_lose_storage = buildOption("仓库丢失量",[{type:"category",name:"日期",data:[]}],[{type:"value",name:"数量"}],{data:["店铺数","设备数"]},[{name:"数量",data:[], type:"line"}]);
var option_lose_ing = buildOption("在途丢失量",[{type:"category",name:"日期",data:[]}],[{type:"value",name:"数量"}],{data:["设备数"]},[{name:"数量",data:[], type:"line"}]);
var option_scrap = buildOption("报废量",[{type:"category",name:"日期",data:[]}],[{type:"value",name:"数量"}],{data:["店铺/仓库数","设备数"]},[{name:"数量",data:[], type:"line"}]);

chart_install.setOption(option_install);
chart_repair.setOption(option_repair);
chart_lose_shop.setOption(option_lose_shop);
chart_lose_storage.setOption(option_lose_storage);
chart_lose_ing.setOption(option_lose_ing);
chart_scrap.setOption(option_scrap);
seachRow();
//loadEchartData(chart_install,option_install,"daySum",{pageSize:10,pageIndex:0,businessType:"1"});
//loadEchartData(chart_repair,option_repair,"daySum",{pageSize:10,pageIndex:0,businessType:"2"});
//loadEchartData(chart_lose,option_lose,"daySum",{pageSize:10,pageIndex:0,businessType:"3"});
//loadEchartData(chart_scrap,option_scrap,"daySum",{pageSize:10,pageIndex:0,businessType:"4"});

function buildOption(titlename,xAxis,yAxis,legend,series)	{
	var option = {
			title:{
				text:titlename
			},
			tooltip : {
		           trigger: 'axis',
		        },
			xAxis: xAxis,
			yAxis: yAxis,
			legend: legend,
			series: series
	}
	
	return option;
}
	
function loadEchartData(chart,option,datakey,urlpam){
	$.ajax({
		url:projectName+"/ReportInventoryAnalyzeAdmin/queryAnalyzeData",
		data:urlpam,
		cache:false,
		dataType:'json',
		success:function(result){
			
			if(urlpam.businessType == "31"){
				option.series = [{
					name:"店铺数",
					type:"line",
					data:result.result.shopSum
				},{
					name:"设备数",
					type:"line",
					data:result.result.eqSum
				}];
				option.xAxis = [{
					type : 'category',
		            boundaryGap : false,
					name:"日期",
					data:result.result.time,
				}];
			}else if(urlpam.businessType == "32"){
				option.series = [{
					name:"仓库数",
					type:"line",
					data:result.result.shopSum
				},{
					name:"设备数",
					type:"line",
					data:result.result.eqSum
				}];
				option.xAxis = [{
					type : 'category',
		            boundaryGap : false,
					name:"日期",
					data:result.result.time,
				}];
			}else if(urlpam.businessType == "33"){
				option.series = [{
					name:"设备数",
					type:"line",
					data:result.result.eqSum
				}];
				option.xAxis = [{
					type : 'category',
		            boundaryGap : false,
					name:"日期",
					data:result.result.time,
				}];
			}else if(urlpam.businessType== 4){
				option.series = [{
					name:"店铺/仓库数",
					type:"line",
					data:result.result.shopSum
				},{
					name:"设备数",
					type:"line",
					data:result.result.eqSum
				}];
				option.xAxis = [{
					type : 'category',
		            boundaryGap : false,
					name:"日期",
					data:result.result.time,
				}];
			}else {
				option.series = [{
					name:"店铺数",
					type:"line",
					data:result.result.shopSum
				},{
					name:"设备数",
					type:"line",
					data:result.result.eqSum
				}];
				option.xAxis = [{
					type : 'category',
		            boundaryGap : false,
					name:"日期",
					data:result.result.time,
				}];
			}
				chart.setOption(option);
		},error:function(){
			mini.alert("加载数据失败");
		}
	});
	chart.setOption(option);
}	

function buildParam(json1, json2){
	for(key in json2){
		json1[key] = json2[key];
	}
	return json1;
}

function seachRow(){
	var form = new mini.Form("ReportInventoryAnalyzeListForm");
	var json = form.getData(true, false);
	loadEchartData(chart_install,option_install,"daySum",buildParam({pageSize:10,pageIndex:0,businessType:"1"},json));
	loadEchartData(chart_repair,option_repair,"daySum",buildParam({pageSize:10,pageIndex:0,businessType:"2"},json));
	loadEchartData(chart_lose_shop,option_lose_shop,"daySum",buildParam({pageSize:10,pageIndex:0,businessType:"31"},json));
	loadEchartData(chart_lose_storage,option_lose_storage,"daySum",buildParam({pageSize:10,pageIndex:0,businessType:"32"},json));
	loadEchartData(chart_lose_ing,option_lose_ing,"daySum",buildParam({pageSize:10,pageIndex:0,businessType:"33"},json));
	loadEchartData(chart_scrap,option_scrap,"daySum",buildParam({pageSize:10,pageIndex:0,businessType:"4"},json));

}

function resetForm(){
	 var form = new mini.Form("#ReportInventoryAnalyzeListForm");
     form.reset();
}

function setDefaultDate(){
	var date = new Date();
	var time = date.getTime();
	time = time-9*24*60*60*1000;
	var d = new Date(time);
	mini.get("dateStart").setValue(dateToString(d));
}
function dateToString(now){  
    var year = now.getFullYear();  
    var month =(now.getMonth() + 1).toString();  
    var day = (now.getDate()).toString();  
    if (month.length == 1) {  
        month = "0" + month;  
    }  
    if (day.length == 1) {  
        day = "0" + day;  
    }  
     var dateTime = year + "-" + month + "-" + day;  
     return dateTime;  
  }  